// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   Entity.java

package tool.radmodel;

import java.sql.*;
import java.util.ArrayList;

// 初始化实体的名字，查询方法，表字段...

public class Entity
{

	private String entityName;
	private String primaryKeyFieldName;
	private String primaryKeyPropertyName;
	private Connection connection;
	private String tableName;
	private Field primaryKeyField;
	private ArrayList<Field> fields;
	private String entityBeanName;
	private String selectSqlName;
	private String selectListSqlName;
	private String selectCountSqlName;
	private String insertSqlName;
	private String insertListSqlName;
	private String updateSqlName;
	private String deleteSqlName;
	private String selectByPkName;
	private String deleteByPkName;

	public Entity(Connection connection, String tableName)
	{
		//获取实体类的名字
		entityName = ColumnConvert.getJavaBeanNameBy(tableName);
		//初始化连接
		this.connection = connection;
		this.tableName = tableName;
		fields = new ArrayList<Field>();
		//将实体的首写字母改成大写
		entityBeanName = entityName.substring(0, 1).toLowerCase()+entityName.substring(1);
		//初始化查询方法
		selectListSqlName = "selectList";
		selectCountSqlName = "selectCount";
		selectSqlName = "select";
		insertSqlName = "insert";
		insertListSqlName = "insertList";
		updateSqlName = "update";
		deleteSqlName = "deleteByMap";
		selectByPkName="selectByPk";
		deleteByPkName="deleteByPk";
		return;
	}

	//查询到数据库所有的字段，以及字段类型属性，然后赋值给对应的实体属性
	public void parseEntityInfo()
		throws SQLException
	{
		Statement stmt = connection.createStatement();
		ResultSet result = stmt.executeQuery((new StringBuilder("select * from ")).append(tableName).append(" where 1 = 2 ").toString());
		ResultSetMetaData metData = result.getMetaData();
		System.out.println("metData:"+metData);
		int ColumnCount = metData.getColumnCount();
		DatabaseMetaData databaseMetaData = connection.getMetaData();
		ResultSet priKeySet = databaseMetaData.getPrimaryKeys(null, null, tableName);
		if (priKeySet.next())
		{
			primaryKeyFieldName = priKeySet.getString(4);
			primaryKeyPropertyName = ColumnConvert.getJavaBeanPropsNameBy(primaryKeyFieldName);
			primaryKeyField = new Field(primaryKeyFieldName, primaryKeyPropertyName, true);
		}
		if(primaryKeyFieldName==null){
			primaryKeyFieldName="id";
			primaryKeyPropertyName="id";
			primaryKeyField = new Field(primaryKeyFieldName, primaryKeyPropertyName, true);
		}
		priKeySet.close();
		for (int i = 1; i <= ColumnCount; i++)
		{
			Object dataType=metData.getColumnClassName(i);
			System.out.println(dataType);
			String fieldName = metData.getColumnName(i);
			int iColumnType = metData.getColumnType(i);
			String iColumnTypeName=metData.getColumnTypeName(i);
			if (!fieldName.equalsIgnoreCase(primaryKeyFieldName))
			{
				String propertyName = ColumnConvert.getJavaBeanPropsNameBy(fieldName);
				Field field = new Field(fieldName, propertyName, false);
				field.setDataType(iColumnType);
				field.setDateTypeName(iColumnTypeName);
				fields.add(field);
			} else
			if (primaryKeyField != null){
				primaryKeyField.setDataType(iColumnType);
				primaryKeyField.setDateTypeName(iColumnTypeName);
			}
				
		}

		result.close();
		stmt.close();
	}

	public Field getPrimaryKeyField()
	{
		return primaryKeyField;
	}

	public String getEntityName()
	{
		return entityName;
	}

	public String getTableName()
	{
		return tableName;
	}

	public ArrayList<Field> getFields()
	{
		return fields;
	}

	public String getEntityBeanName()
	{
		return entityBeanName;
	}

	public String getSelectSqlName()
	{
		return selectSqlName;
	}


	public String getInsertSqlName()
	{
		return insertSqlName;
	}

	public String getUpdateSqlName()
	{
		return updateSqlName;
	}

	public String getDeleteSqlName()
	{
		return deleteSqlName;
	}

	public String getSelectListSqlName() {
		return selectListSqlName;
	}

	public String getInsertListSqlName() {
		return insertListSqlName;
	}

	public String getSelectByPkName() {
		return selectByPkName;
	}

	public String getDeleteByPkName() {
		return deleteByPkName;
	}

	public String getSelectCountSqlName() {
		return selectCountSqlName;
	}

	public void setSelectCountSqlName(String selectCountSqlName) {
		this.selectCountSqlName = selectCountSqlName;
	}

}
